package com.bq.gametrade.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.GameTradeApplication;
import com.bq.gametrade.R;
import com.bq.gametrade.bean.RedEnvelopeListDetails;
import com.bq.gametrade.utils.Base64Util;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * @author Feel on 2016/11/29 11:05
 */

public class RedEnvelopeListAdapter extends BaseAdapter {



    private ArrayList<RedEnvelopeListDetails> data;
    private Context mContext;
    private GameTradeApplication application;



    //ListView视图的内容由IMsgViewType决定
    public static interface IMsgViewType {
        //对方发来的信息
        int IMVT_COM_MSG = 0;
        //自己发出的信息
        int IMVT_TO_MSG = 1;
    }

    /**
     * 红包ItemClick的回调接口
     *
     * @author feel
     */
    public interface OnItemPressClickListener {
        void onItemPressClick(int position);
    }

    private OnItemPressClickListener mOnItemClickListener;

    public void setOnItemPressClickLitenerr(OnItemPressClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 红包ItemClick的回调接口
     *
     * @author feel
     */
    public interface OnHeadImagePressClickListener {
        void OnHeadImageClick(int position);
    }

    private OnHeadImagePressClickListener onHeadImagePressClickListener;

    public void setOnHeadImagePressClickListener(OnHeadImagePressClickListener onHeadImagePressClickListener) {
        this.onHeadImagePressClickListener = onHeadImagePressClickListener;
    }


    public RedEnvelopeListAdapter(Context context, ArrayList<RedEnvelopeListDetails> list, GameTradeApplication application) {
        this.mContext = context;
        this.data = list;
        this.application = application;

    }

    public void setData(ArrayList<RedEnvelopeListDetails> list,GameTradeApplication application){
        this.data = list;
        this.application = application;
    }

    //获取ListView的项个数
    public int getCount() {
        return data.size();
    }

    //获取项
    public Object getItem(int position) {
        return data.get(position);
    }

    //获取项的ID
    public long getItemId(int position) {
        return position;
    }

    //获取项的类型
    public int getItemViewType(int position) {
        RedEnvelopeListDetails details = data.get(position);
        if (details.revice_user.equals(application.getUserInfo().account)) {
            return IMsgViewType.IMVT_COM_MSG;
        } else {
            return IMsgViewType.IMVT_TO_MSG;
        }

    }

    //获取项的类型数
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        RedEnvelopeListDetails details = data.get(position);
        if (convertView == null) {
            if (details.revice_user.equals(application.getUserInfo().account)) {
                //如果是对方发来的消息，则显示的是左气泡
                convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_sendchat_item_friend, null);
            } else {
                //如果是自己发出的消息，则显示的是右气泡
                convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_edrenvelopelist_item_my, null);
            }
            viewHolder = new ViewHolder();
            viewHolder.userHeadImage = (ImageView) convertView.findViewById(R.id.civ_headImage);
            viewHolder.layRedEnvelope = (LinearLayout) convertView.findViewById(R.id.lay_redEnvelope);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_ctime);
            viewHolder.tvLeaveMessage = (TextView) convertView.findViewById(R.id.tv_leaveMessage);
            viewHolder.tvRedEnvelopeWay = (TextView) convertView.findViewById(R.id.tv_redEnvelopeWay) ;
            viewHolder.ivRedEnvelopeType = (ImageView) convertView.findViewById(R.id.iv_redEnvelopeType);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvTime.setText(data.get(position).ctime);
        viewHolder.tvLeaveMessage.setText(Base64Util.decodeStr(details.title));
        viewHolder.layRedEnvelope.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemPressClick(position);
            }
        });
        viewHolder.userHeadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onHeadImagePressClickListener.OnHeadImageClick(position);
            }
        });
        if(details.revice_user.equals(application.getUserInfo().account)){
            Glide.with(mContext)
                    .load(details.send_img)
                    .error( R.mipmap.user_headimg_gray )
                    .into(viewHolder.userHeadImage);
            viewHolder.tvRedEnvelopeWay.setText("领取红包");
        }else if(details.send_user.equals(application.getUserInfo().account)){
            viewHolder.tvRedEnvelopeWay.setText("查看红包");
            Glide.with(mContext).load(application.getUserInfo().headimg ).placeholder( R.mipmap.user_headimg_gray ).error( R.mipmap.user_headimg_gray ).into(viewHolder.userHeadImage);
        }
        if(details.type.equals("0")){
            viewHolder.ivRedEnvelopeType.setImageResource(R.mipmap.icon_red_envelope_money);
        }else if(details.type.equals("1")){
            viewHolder.ivRedEnvelopeType.setImageResource(R.mipmap.icon_red_envelope_ticket);
        }
        return convertView;
    }

    class ViewHolder {
        ImageView userHeadImage;
        LinearLayout  layRedEnvelope;
        TextView tvTime;
        TextView tvLeaveMessage;
        TextView tvRedEnvelopeWay;
        ImageView ivRedEnvelopeType;
    }

}
